import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/knowledgeword.dart';
import 'package:ichinesereader/widgets/buttons/basicWordButtonBar.dart';
import 'package:ichinesereader/widgets/cards/word.dart';
import 'package:hive_flutter/hive_flutter.dart';

List<GlobalKey<WordCardState>> wordCardKeys = [];
bool disableWordCardAudioPlay = false;

class BasicWords extends StatefulWidget {
  BasicWords({Key? key, this.folderId, this.level, this.isMainFolder})
      : super(key: key);
  final String? folderId;
  final String? level;
  final bool? isMainFolder;
  static const String routeName = '/student/basicCardList';

  @override
  State<BasicWords> createState() => _BasicWordsState();
}

class _BasicWordsState extends State<BasicWords> {
  final KnowledgeWordsController _controller =
      Get.put(KnowledgeWordsController());
  final box = Hive.box('appconfig');

  @override
  void initState() {
    _controller.fetchKnowledgeFolderOnlyTree();
    Future.delayed(Duration(milliseconds: 500), () {
      _controller.fetchBasicKnowledgeWords();
    });
    super.initState();
  }

  @override
  void dispose() {
    Get.delete<KnowledgeWordsController>();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: _controller.obx((data) {
        return Column(children: [
          Container(
            margin: EdgeInsets.all(
              12.0,
            ),
            child: BasicWordButtonBar(),
          ),
          Expanded(
            child: _controller.obx(
              (data) {
                wordCardKeys = [];
                return GridView.builder(
                    gridDelegate:
                        const SliverGridDelegateWithMaxCrossAxisExtent(
                            maxCrossAxisExtent: 300,
                            childAspectRatio: 156 / 200,
                            crossAxisSpacing: 12,
                            mainAxisSpacing: 12),
                    itemCount: _controller.knowledgeWords.length,
                    itemBuilder: (context, index) {
                      var ckey = GlobalKey<WordCardState>();

                      if (_controller.knowledgeWords[index].id != null &&
                          _controller.knowledgeWords[index].id != '')
                        wordCardKeys.add(ckey);

                      return WordCard(
                        flashcard: _controller.knowledgeWords[index],
                        key: ckey,
                      );
                    });
              },
              onEmpty: Center(child: Text('No cards or folders'.tr)),
              onLoading: Container(),
            ),
          ),
        ]);
      }),
    );
  }
}
